package com.example.javaspringweb.controller;




import com.example.javaspringweb.entity.UserDemo;
import com.example.javaspringweb.service.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;



@Controller
public class LoginController {

    @Resource
    private UserServiceImpl userServiceImpl;

    @RequestMapping("/user/login")
    public String login(// @RequestParam 请求参数 Model model 回写参数
                        @RequestParam String username,
                        @RequestParam String password,
                        Model model,
                        HttpSession session) {// 具体业务

        UserDemo userDemo = userServiceImpl.userLogin(username, password);

        if (userDemo != null) {
            session.setAttribute("userDemo", userDemo);
            return "redirect:/homepage.html";  // redirect 重定向
        } else {
            // 告诉用户 登录失败
            model.addAttribute("msg", "用户名或者密码错误！");
            return "login";
        }
    }


}

